// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

#include <nebula/array/array_primitive.h>

#include <cstdint>
#include <memory>

#include <nebula/array/array_base.h>
#include <nebula/types/type.h>
#include <nebula/bits/bit_block_counter.h>
#include <nebula/bits/bitmap_ops.h>
#include <turbo/log/logging.h>

namespace nebula {

    // ----------------------------------------------------------------------
    // Primitive array base

    PrimitiveArray::PrimitiveArray(const std::shared_ptr<DataType> &type, int64_t length,
                                   const std::shared_ptr<Buffer> &data,
                                   const std::shared_ptr<Buffer> &null_bitmap,
                                   int64_t null_count, int64_t offset) {
        set_data(ArrayData::create(type, length, {null_bitmap, data}, null_count, offset));
    }

    // ----------------------------------------------------------------------
    // BooleanArray

    BooleanArray::BooleanArray(const std::shared_ptr<ArrayData> &data)
            : PrimitiveArray(data) {
        KCHECK_EQ(data->type->id(), Type::BOOL);
    }

    BooleanArray::BooleanArray(int64_t length, const std::shared_ptr<Buffer> &data,
                               const std::shared_ptr<Buffer> &null_bitmap, int64_t null_count,
                               int64_t offset)
            : PrimitiveArray(boolean(), length, data, null_bitmap, null_count, offset) {}

    int64_t BooleanArray::false_count() const {
        return this->length() - this->null_count() - this->true_count();
    }

    int64_t BooleanArray::true_count() const {
        if (data_->may_have_nulls()) {
            DKCHECK(data_->buffers[0]);
            return internal::CountAndSetBits(data_->buffers[0]->data(), data_->offset,
                                             data_->buffers[1]->data(), data_->offset,
                                             data_->length);
        } else {
            return internal::CountSetBits(data_->buffers[1]->data(), data_->offset,
                                          data_->length);
        }
    }

    // ----------------------------------------------------------------------
    // Day time interval

    DayTimeIntervalArray::DayTimeIntervalArray(const std::shared_ptr<ArrayData> &data) {
        set_data(data);
    }

    DayTimeIntervalArray::DayTimeIntervalArray(const std::shared_ptr<DataType> &type,
                                               int64_t length,
                                               const std::shared_ptr<Buffer> &data,
                                               const std::shared_ptr<Buffer> &null_bitmap,
                                               int64_t null_count, int64_t offset)
            : PrimitiveArray(type, length, data, null_bitmap, null_count, offset) {}

    DayTimeIntervalArray::DayTimeIntervalArray(int64_t length,
                                               const std::shared_ptr<Buffer> &data,
                                               const std::shared_ptr<Buffer> &null_bitmap,
                                               int64_t null_count, int64_t offset)
            : PrimitiveArray(day_time_interval(), length, data, null_bitmap, null_count, offset) {
    }

    DayTimeIntervalType::DayMilliseconds DayTimeIntervalArray::get_value(int64_t i) const {
        DKCHECK(i < length());
        return *reinterpret_cast<const DayTimeIntervalType::DayMilliseconds *>(
                raw_values_ + (i + data_->offset) * byte_width());
    }

    // ----------------------------------------------------------------------
    // Month, day and Nanos interval

    MonthDayNanoIntervalArray::MonthDayNanoIntervalArray(
            const std::shared_ptr<ArrayData> &data) {
        set_data(data);
    }

    MonthDayNanoIntervalArray::MonthDayNanoIntervalArray(
            const std::shared_ptr<DataType> &type, int64_t length,
            const std::shared_ptr<Buffer> &data, const std::shared_ptr<Buffer> &null_bitmap,
            int64_t null_count, int64_t offset)
            : PrimitiveArray(type, length, data, null_bitmap, null_count, offset) {}

    MonthDayNanoIntervalArray::MonthDayNanoIntervalArray(
            int64_t length, const std::shared_ptr<Buffer> &data,
            const std::shared_ptr<Buffer> &null_bitmap, int64_t null_count, int64_t offset)
            : PrimitiveArray(month_day_nano_interval(), length, data, null_bitmap, null_count,
                             offset) {}

    MonthDayNanoIntervalType::MonthDayNanos MonthDayNanoIntervalArray::get_value(
            int64_t i) const {
        DKCHECK(i < length());
        return *reinterpret_cast<const MonthDayNanoIntervalType::MonthDayNanos *>(
                raw_values_ + (i + data_->offset) * byte_width());
    }

}  // namespace nebula
